import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/controller/BuildController.dart';
import 'package:flutter_house_manager/utils/PromptAction.dart';
import 'package:get/get.dart';

class RoomList extends StatefulWidget {
  const RoomList({super.key});

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  BuildController buildController = Get.find();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _mockData();
  }

  List _list = [];
  _mockData() {
    //先生成楼层的随机数
    Random r = Random();
    int floor = r.nextInt(6) + 1; // 楼层随机数
    int roomCount = r.nextInt(5) + 2; // 房间的随机数
    List list = [];
    for (int i = 0; i < roomCount; i++) {
      list.add('${floor}0${i + 1}');
    }
    _list = list;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BuildController>(builder: (controller) {
      return Scaffold(
          appBar: AppBar(
            title: const Text('选择房间'),
          ),
          body: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: GestureDetector(
                    onTap: () {
                      // 拿到对应的房间号 放入到getx
                      buildController.buildInfo["room"] = _list[index]; //
                      // 把房间号放入到共享参数
                      buildController
                          .updateBuildInfo(buildController.buildInfo);

                      PromptAction.showToast(
                          buildController.buildInfo["point"] +
                              buildController.buildInfo["build"] +
                              buildController.buildInfo["room"]);
                    },
                    child: Row(children: [
                      Expanded(
                          child: Text(controller.buildInfo["point"] +
                              controller.buildInfo["build"] +
                              _list[index] +
                              "室")),
                      const Row(children: [
                        Icon(Icons.arrow_forward_ios,
                            size: 16, color: Colors.black),
                      ])
                    ]),
                  ));
            },
            itemCount: _list.length,
          ));
    });
  }
}
